Skip to content

Add stdext module for swoole-cli and enhance PHP standard library #5842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 36 commits into from
Aug 11, 2025

Conversation

matyhtf
Copy link
Member

@matyhtf matyhtf commented Aug 11, 2025

1. Add support for type arrays

List

$array = typed_array('<int>');
$array[] = 123;

try {
    $array[] = '456';
} catch (TypeError $e) {
    Assert::true($e->getMessage()->contains('Array value type mismatch'));
}

Map

$array = typed_array('<int, string>');
$array[999] = 'test';

try {
    $array['hello'] = 'world';
} catch (TypeError $e) {
    Assert::true($e->getMessage()->contains('Array key type mismatch'));
    echo "DONE\n";
}

2. Allow built-in methods for array/string/stream (resource)

String

$string = 'hello world, this is a test string';
Assert::false($string->isEmpty());
Assert::eq($string->length(), strlen($string));
Assert::eq($string->substr(0, 5), 'hello');
Assert::eq($string->contains('world'), true);
Assert::eq($string->indexOf('test'), strpos($string, 'test'));
Assert::eq($string->split(' '), explode(' ', $string));
Assert::true(''->isEmpty());

Array

$array = typed_array('<int>');
$array[0] = 1;
$array[1] = 2;
$array[2] = 3;
$array[3] = 999;
Assert::false($array->isEmpty());
Assert::eq($array->count(), count($array));
Assert::eq($array->slice(0, 2), [1, 2]);
Assert::true($array->contains(999));
Assert::eq($array->search(999), 3);
Assert::true([]->isEmpty());

Stream

$filepath = "/tmp/test.txt";
$fp = fopen($filepath, "w+");
$rdata = random_bytes(1024);
Assert::greaterThan($fp->write($rdata->base64Encode()), $rdata->length());
$fp->seek(0);
Assert::eq($rdata, $fp->read(8192)->base64Decode());

NathanFreeman and others added 2 commits August 11, 2025 11:13
* optimize feature

* Update swoole_stdext.cc

---------

Co-authored-by: Tianfeng.Han <[email protected]>
@matyhtf matyhtf requested a review from Copilot August 11, 2025 08:11
Copilot

This comment was marked as outdated.

@matyhtf matyhtf requested a review from Copilot August 11, 2025 08:37
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a new stdext module for swoole-cli that enhances the PHP standard library by adding support for typed arrays and enabling built-in method calls directly on arrays, strings, and streams. The module allows users to define strongly-typed arrays using syntax like typed_array('<int>') and call methods like $string->length() instead of using traditional functions like strlen($string).

  • Adds typed array functionality with runtime type checking for keys and values
  • Implements method call syntax for arrays, strings, and stream resources
  • Adds comprehensive test coverage for all new functionality

Reviewed Changes

Copilot reviewed 52 out of 52 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
thirdparty/php/zend/zend_execute.c Adds Zend VM execution helper functions for array operations
thirdparty/php/standard/var_decoder.cc Adds support for non-backed enum JSON error handling
ext-src/swoole_stdext.cc Main implementation of the stdext module with typed arrays and method calling
ext-src/php_swoole_stdext.h Header file defining function prototypes for stdext functionality
ext-src/stubs/php_swoole_stdext.stub.php PHP stub definitions for IDE support
Multiple test files Comprehensive test coverage for typed arrays, string methods, and array methods
Build configuration files Updates to enable stdext compilation option

SW_EXTERN_C_BEGIN
#include "ext/pcre/php_pcre.h"
#include "ext/json/php_json.h"
#include "thirdparty/php/zend/zend_execute.c"
Copy link
Preview

Copilot AI Aug 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Including a .c file directly is unusual and can lead to multiple definition errors. Consider moving the required code to a header file or linking it properly during compilation.

Suggested change
#include "thirdparty/php/zend/zend_execute.c"
#include "thirdparty/php/zend/zend_execute.h"

Copilot uses AI. Check for mistakes.

}

zval count = {};
php_pcre_pce_incref(pce);
Copy link
Preview

Copilot AI Aug 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Version-specific conditional compilation blocks should be documented to explain what functionality differs between versions and why the version check is necessary.

Suggested change
php_pcre_pce_incref(pce);
php_pcre_pce_incref(pce);
/*
* PHP 8.4.0 and later changed the signature of php_pcre_match_impl:
* - The 'is_global' argument is now a boolean (global == 1), and the 'use_flags' argument was removed.
* - For earlier versions, both 'global' and 'use_flags' (ZEND_NUM_ARGS() >= 3) must be passed.
* This conditional ensures compatibility with both versions.
*/

Copilot uses AI. Check for mistakes.

Copy link

codecov bot commented Aug 11, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (master@1bfe608). Learn more about missing BASE report.
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##             master    #5842   +/-   ##
=========================================
  Coverage          ?   86.18%           
=========================================
  Files             ?      109           
  Lines             ?    16820           
  Branches          ?     2975           
=========================================
  Hits              ?    14497           
  Misses            ?     2323           
  Partials          ?        0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@matyhtf matyhtf merged commit 809eb82 into master Aug 11, 2025
88 of 96 checks passed
@matyhtf matyhtf deleted the stdext branch August 11, 2025 09:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants